/*
File:		GameplayState.cpp
Course:		SGD 1301
Author:		Peter Nguyen
Purpose:	CGameplayState class handles the gameplay
*/

#include "GameplayState.h"
#include "PauseState.h"

#include "Game.h"

#include "PowerUp.h"

#include "../SGD Wrappers/CSGD_Direct3D.h"
#include "../SGD Wrappers/CSGD_DirectInput.h"
#include "../SGD Wrappers/CSGD_TextureManager.h"
#include "../SGD Wrappers/CSGD_XAudio2.h"
#include "../SGD Wrappers/CSGD_MessageSystem.h"
#include "../SGD Wrappers/CSGD_EventSystem.h"

#include "../SGD Wrappers/SGD_String.h"
#include "../SGD Wrappers/SGD_Math.h"

#include "EntityManager.h"
#include "Entity.h"
#include "Ship.h"
#include "InputShip.h"
#include "AIShip.h"
#include "Laser.h"
#include "CreateSpreadLaserMessage.h"
#include "Tiles.h"

#include "Message.h"
#include "CreateLaserMessage.h"
#include "CreateEnemyMessage.h"
#include "DestroyEntityMessage.h"

#include "CharacterSelectionState.h"
#include "CreditState.h"

#include "BitmapFont.h"

#include "MainMenuState.h"

#include <cassert>

#include <random>

#include <iostream>
#include <fstream>
#include <string>

#define WORLD_SCALE 7.0f
#define WORLD_IMAGE_WIDTH 2056
#define WORLD_IMAGE_HEIGHT 2056

////////////////////////////////////////////////////////////
// SINGLETON!
/*static*/ CGameplayState* CGameplayState::GetInstance( void )
{
	// Static local variable
	static CGameplayState s_Instance;

	// Return the ADDRESS
	return &s_Instance;
}

struct ParallaxImage
{
public:
	int x;
	int y;
	int imageID;
	ParallaxImage()
	{
		x = 0;
		y = 0;
	}
	void Update(float fElapsedTime )
	{
		CGame* gamePtr = CGame::GetInstance();
		int w = gamePtr->GetWidth();
		x+=1;
		y+=1;
		if(x>w)
			x-=w;
		else if(x<0)
			x+=w;
		if(y<0)
			y+=w;
		else if(y>w)
			y-=w;
	}
	void Render()
	{
		CGame* gamePtr = CGame::GetInstance();
		CSGD_TextureManager* m_pTM = CSGD_TextureManager::GetInstance();
		int w = gamePtr->GetWidth();


		float scaleX = w / 512.0f;
		float scaleY = w / 512.0f;
		m_pTM->Draw(imageID,x-w,y-w,scaleX,scaleY,nullptr,512.0f,512.0f,0,D3DCOLOR_ARGB(200,255,250,250));
		m_pTM->Draw(imageID,x,y-w,scaleX,scaleY,nullptr,512.0f,512.0f,0,D3DCOLOR_ARGB(200,255,250,250));
		m_pTM->Draw(imageID,x-w,y,scaleX,scaleY,nullptr,512.0f,512.0f,0,D3DCOLOR_ARGB(200,255,250,250));
		m_pTM->Draw(imageID,x,y,scaleX,scaleY,nullptr,512.0f,512.0f,0,D3DCOLOR_ARGB(200,255,250,250));
	}
};

////////////////////////////////////////////////////////////
// CONSTRUCTOR
CGameplayState::CGameplayState(void)
{
	// Set the data members to 'clean' values
	m_pD3D			= nullptr;
	m_pDI			= nullptr;
	m_pTM			= nullptr;
	m_pXA			= nullptr;
	m_pMS			= nullptr;
	m_pEM			= nullptr;
	m_pFont			= nullptr;

	// Invalid Asset == -1
	for(int i = 0; i < 5; ++i)
		m_nShipImageID[i] = -1;
	m_nLaserImageID			= -1;
	m_nBackgroundMusicID	= -1;
	m_nLaserShotSfxID		= -1;
	m_nVictorySfxID			= -1;
	m_nLoseSfxID			= -1;
	m_nBackgroundID			= -1;
	m_nWhiteDotID			= -1;
	m_nWinScreenID			= -1;
	m_nLoseScreenID			= -1;
	m_nGoalID				= -1;
	m_nBoundID				= -1;
	m_nMistID				= -1;
	m_nPowerUp1ID				= -1;
	m_nPowerUp2ID				= -1;
	m_nPowerUp3ID				= -1;
	m_nPowerUp4ID				= -1;
	m_pShip			= nullptr;
	m_fSpawnRate = 1.0f;
	m_fSpawnTimer = m_fSpawnRate;

	m_nFPS			= 0;
	m_nFrames		= 0;
	m_fFPSTimer		= 0.0f;



}

////////////////////////////////////////////////////////////
// DESTRUCTOR
CGameplayState::~CGameplayState(void)
{
}

////////////////////////////////////////////////////////////
// Enter				- load resources
/*virtual*/ void CGameplayState::Enter( void )
{
	// Store the wrapper singletons
	m_pD3D			= CSGD_Direct3D::GetInstance();
	m_pDI			= CSGD_DirectInput::GetInstance();
	m_pTM			= CSGD_TextureManager::GetInstance();
	m_pXA			= CSGD_XAudio2::GetInstance();
	m_pMS			= CSGD_MessageSystem::GetInstance();
	m_pEM			= CEntityManager::GetInstance();
	m_pFont			= CGame::GetInstance()->GetFont();

	// Initialize Message System
	m_pMS->InitMessageSystem( &MessageProc );



	// ASSET PATHS ARE RELATIVE TO THE PROJECT (.vcxproj)!!!

	// Load texture:
	// - the color key will become transparent (removes the black background)
	m_nShipImageID[0]		= m_pTM->LoadTexture( _T("resource/graphics/PN_ship1.png"));
	m_nShipImageID[1]		= m_pTM->LoadTexture( _T("resource/graphics/PN_ship2.png"));
	m_nShipImageID[2]		= m_pTM->LoadTexture( _T("resource/graphics/PN_ship3.png"));
	m_nShipImageID[3]		= m_pTM->LoadTexture( _T("resource/graphics/PN_ship4.png"));
	m_nShipImageID[4]		= m_pTM->LoadTexture( _T("resource/graphics/PN_ship5.png"));
	m_nLaserImageID			= m_pTM->LoadTexture( _T("resource/graphics/PN_lazer.png"));
	m_nBackgroundID			= m_pTM->LoadTexture( _T("resource/graphics/PN_BG1.jpg"));
	m_nWhiteDotID			= m_pTM->LoadTexture( _T("resource/graphics/PN_WhiteDot.png"));
	m_nWinScreenID			= m_pTM->LoadTexture( _T("resource/graphics/PN_WinningScreen.jpg"));
	m_nLoseScreenID			= m_pTM->LoadTexture( _T("resource/graphics/PN_LoseScreen.png"));
	m_nGoalID				= m_pTM->LoadTexture( _T("resource/graphics/PN_Goal.png"));
	m_nBoundID				= m_pTM->LoadTexture( _T("resource/graphics/PN_Bound.png"));
	m_nMistID				= m_pTM->LoadTexture( _T("resource/graphics/PN_Mist.png"));
	m_nPowerUp1ID				= m_pTM->LoadTexture( _T("resource/graphics/PN_PowerUp1.png"));
	m_nPowerUp2ID				= m_pTM->LoadTexture( _T("resource/graphics/PN_PowerUp2.png"));
	m_nPowerUp3ID				= m_pTM->LoadTexture( _T("resource/graphics/PN_PowerUp3.png"));
	m_nPowerUp4ID				= m_pTM->LoadTexture( _T("resource/graphics/PN_PowerUp4.png"));

	m_nBackgroundMusicID	= m_pXA->MusicLoadSong( _T("resource/sounds/PN_MUS_Background.xwm") );
	m_nLaserShotSfxID		= m_pXA->SFXLoadSound( _T( "resource/sounds/PN_SFX_LaserShot.wav") );
	m_nVictorySfxID			= m_pXA->SFXLoadSound( _T( "resource/sounds/PN_SFX_Victory.wav") );
	m_nLoseSfxID			= m_pXA->SFXLoadSound( _T( "resource/sounds/PN_SFX_Lose.wav") );

	m_pXA->MusicSetSongVolume(m_nBackgroundMusicID,CGame::GetInstance()->GetBGMVolume()/100.0f);
	m_pXA->SFXSetSoundVolume(m_nLaserShotSfxID,CGame::GetInstance()->GetSoundVolume()/100.0f);
	m_pXA->SFXSetSoundVolume(m_nVictorySfxID,CGame::GetInstance()->GetSoundVolume()/100.0f);
	m_pXA->SFXSetSoundVolume(m_nLoseSfxID,CGame::GetInstance()->GetSoundVolume()/100.0f);

	m_pXA->MusicPlaySong( m_nBackgroundMusicID, true );

	LoadTiles();

	std::vector<SCharacterItem*> _sci = CCharacterSelection::GetInstance()->characterVector;
	int pSelIndex = CCharacterSelection::GetInstance()->GetCharacterSelectionPositon();

	for(int i = 0; i < 5; ++i)
	{
		CShip * ai_Ship;
		if(i==pSelIndex)
		{
			ai_Ship = new CInputShip; 
			ai_Ship->AddRef();
			m_pShip = ai_Ship;
		}
		else
		{
			ai_Ship	= new CAIShip();
		}

		ai_Ship->SetImageID(m_nShipImageID[i%5]);
		ai_Ship->SetMaxAcceleration(_sci[i%5]->AccelerationRate);
		ai_Ship->SetMaxFireRate(_sci[i%5]->FireRate);
		ai_Ship->SetMaxSpeed(_sci[i%5]->MaxSpeed);
		ai_Ship->SetMaxTurnRate(_sci[i%5]->TurningSpeed);

		ai_Ship->SetWidth( 64 );
		ai_Ship->SetHeight( 64 );

		ai_Ship->SetCameraRot( 3.1415f / 2);
		ai_Ship->SetRotation( 3.1415f / 2);

		int offsetX = 11*64;
		int offsetY = i*128+128*5;

		ai_Ship->SetPosX((float)offsetX);
		ai_Ship->SetPosY((float)offsetY);
		m_pEM->AddEntity((IEntity*)ai_Ship );	

		shipVector.push_back(ai_Ship);
		//add vector scope ref count
		ai_Ship->AddRef();

		//release this scope ref
		ai_Ship->Release();
		ai_Ship = nullptr;
	}

	parallaxImage = new ParallaxImage();
	parallaxImage->imageID = m_nMistID;

	m_nFPS			= 60;
	m_nFrames		= 0;
	m_fFPSTimer		= 0.0f;
	m_nGameOver		= 0;
	m_fGameOverTimer= 4.0f;

	CGame::GetInstance()->SetCameraRotation(0);
	CGame::GetInstance()->SetCameraX(0);
	CGame::GetInstance()->SetCameraY(0);
	CGame::GetInstance()->SetWorldWidth((int)(WORLD_IMAGE_WIDTH*WORLD_SCALE));
	CGame::GetInstance()->SetWorldHeight((int)(WORLD_IMAGE_HEIGHT*WORLD_SCALE));
	CGame::GetInstance()->SetScore(0);
}

void CGameplayState::LoadTiles()
{
	int tileSize = (int)(16*WORLD_SCALE);
	std::ifstream myFile;
	std::string str;
	int col=0,row=0;
	myFile.open("level1.txt");
	if(myFile.is_open())
	{
		std::getline(myFile,str);
		while(!myFile.eof())
		{
			for(unsigned int i = 0; i < str.length(); ++i)
			{
				m_ucMiniMap[col][row] = 0;
				switch(str.at(i))
				{
				case '.':
					{
						CTiles* tile = new CTiles();
						tile->SetWidth(tileSize);
						tile->SetHeight(tileSize);
						tile->SetPosX((float)col);
						tile->SetPosY((float)row);
						tile->SetImageID(m_nBoundID);
						tile->SetHashKey(row*128+col);
						m_pEM->AddEntity(tile);
						tile->Release();
						tile=nullptr;
						m_ucMiniMap[col][row] = 1; 
					}
					break;
				case 'X':

					break;
				case '1':
					{
						CTiles* tile = new CPowerUp(1);
						tile->SetWidth(tileSize);
						tile->SetHeight(tileSize);
						tile->SetPosX((float)col);
						tile->SetPosY((float)row);
						tile->SetImageID(m_nPowerUp1ID);
						tile->SetHashKey(row*128+col);
						m_pEM->AddEntity(tile);
						tile->Release();
						tile=nullptr; 
					}
					break;
				case '2':
					{
						CTiles* tile = new CPowerUp(2);
						tile->SetWidth(tileSize);
						tile->SetHeight(tileSize);
						tile->SetPosX((float)col);
						tile->SetPosY((float)row);
						tile->SetImageID(m_nPowerUp2ID);
						tile->SetHashKey(row*128+col);
						m_pEM->AddEntity(tile);
						tile->Release();
						tile=nullptr;
					}
					break;
				case '3':
					{
						CTiles* tile = new CPowerUp(3);
						tile->SetWidth(tileSize);
						tile->SetHeight(tileSize);
						tile->SetPosX((float)col);
						tile->SetPosY((float)row);
						tile->SetImageID(m_nPowerUp3ID);
						tile->SetHashKey(row*128+col);
						m_pEM->AddEntity(tile);
						tile->Release();
						tile=nullptr;
					}
					break;
				case '4':
					{
						CTiles* tile = new CPowerUp(4);
						tile->SetWidth(tileSize);
						tile->SetHeight(tileSize);
						tile->SetPosX((float)col);
						tile->SetPosY((float)row);
						tile->SetImageID(m_nPowerUp4ID);
						tile->SetHashKey(row*128+col);
						m_pEM->AddEntity(tile);
						tile->Release();
						tile=nullptr;
					}
					break;
				}
				++col;
			}
			col = 0;
			++row;
			std::getline(myFile,str);
		}
		myFile.close();
	}
	m_pEM->LoadWayPoints(tileSize);
}

void CGameplayState::Exit( void )
{
	for(unsigned int i = 0; i < shipVector.size(); ++i)
		shipVector[i]->Release();
	shipVector.clear();

	if( m_pShip != nullptr )
	{
		m_pShip->Release();
		m_pShip = nullptr;
	}

	if( m_pEM != nullptr )
	{
		m_pEM->RemoveAll();
		CEntityManager::DeleteInstance();
		m_pEM = nullptr;
	}

	if( m_pMS != nullptr )
	{
		m_pMS->ShutdownMessageSystem();
		m_pMS = nullptr;
	}
	for(int i = 0; i < 5; ++i)
		UnloadTextureHelper(m_nShipImageID[i]);
	UnloadTextureHelper(m_nLaserImageID);
	UnloadTextureHelper(m_nBackgroundID);
	UnloadTextureHelper(m_nWhiteDotID);
	UnloadTextureHelper(m_nWinScreenID);
	UnloadTextureHelper(m_nLoseScreenID);
	UnloadTextureHelper(m_nGoalID);
	UnloadTextureHelper(m_nBoundID);
	UnloadTextureHelper(m_nMistID);
	UnloadTextureHelper(m_nPowerUp1ID);
	UnloadTextureHelper(m_nPowerUp2ID);
	UnloadTextureHelper(m_nPowerUp3ID);
	UnloadTextureHelper(m_nPowerUp4ID);
	UnloadMusicHelper(m_nBackgroundMusicID);
	UnloadSFXHelper(m_nLaserShotSfxID);
	UnloadSFXHelper(m_nVictorySfxID);	
	UnloadSFXHelper(m_nLoseSfxID);	

	delete parallaxImage;

	m_pD3D			= nullptr;
	m_pDI			= nullptr;
	m_pTM			= nullptr;
	m_pXA			= nullptr;
	m_pMS			= nullptr;
	m_pEM			= nullptr;
	m_pFont			= nullptr;
}

void CGameplayState::UnloadTextureHelper(int&id)
{
	if(id!=-1)
	{
		m_pTM->UnloadTexture(id);
		id = -1;
	}
}
void CGameplayState::UnloadMusicHelper(int&id)
{
	if( id != -1 )
	{
		m_pXA->MusicStopSong( id );
		m_pXA->MusicUnloadSong( id );
		id = -1;
	}
}
void CGameplayState::UnloadSFXHelper(int&id)
{
	if( id != -1 )
	{
		m_pXA->SFXStopSound( id );
		m_pXA->SFXUnloadSound( id );
		id = -1;
	}
}

bool CGameplayState::Input( void )
{
	if( m_pDI->KeyPressed( DIK_9 ) )
		CGame::DEBUG_MODE = !CGame::DEBUG_MODE;

	if( m_pDI->KeyPressed( DIK_ESCAPE ) )
	{
		CGame::GetInstance()->PushState(CPauseState::GetInstance());
		return true;
	}
	else if( m_pDI->KeyPressed( DIK_P))
	{
		CGame::GetInstance()->PushState(CPauseState::GetInstance());
	}
	if(m_fGameOverTimer<=0)
	{
		CGame::GetInstance()->PopState();//character selection
		CGame::GetInstance()->PopState();//main menu
		CGame::GetInstance()->PushState(CCreditState::GetInstance());//credit

	}
	return true;
}			

void CGameplayState::Update( float fElapsedTime )
{
	if(!m_pXA->MusicIsSongPlaying(m_nBackgroundMusicID))
		m_pXA->MusicPlaySong( m_nBackgroundMusicID, true );

	UpdateFPS(fElapsedTime);

	m_pEM->UpdateAll( fElapsedTime );
	m_pEM->CheckAllCollisions();

	m_fSpawnRate-=fElapsedTime;
	if(m_fSpawnRate <= 0)
	{
		//CSGD_MessageSystem::GetInstance()->SendMsg(new CCreateEnemyMessage(m_pShip));
		m_fSpawnRate = m_fSpawnTimer;
	}

	parallaxImage->Update(fElapsedTime);

	m_pMS->ProcessMessages();

	//check win condition
	if(m_nGameOver==0)
	{
		for(unsigned int i = 0; i < shipVector.size();++i)
			if(((CShip*)shipVector[i])->GetTargetWaypoint()>=/*10)//*/(int)CEntityManager::GetInstance()->GetWayPointSize()-3)
			{
				if(shipVector[i]->GetType()==CEntity::ENT_INPUTSHIP)
				{
					m_nGameOver = 1;
					m_fGameOverTimer = 7;
					m_pXA->SFXPlaySound(m_nVictorySfxID,false);
				}
				else if(shipVector[i]->GetType()==CEntity::ENT_AISHIP)
				{
					m_nGameOver = 2;
					m_fGameOverTimer = 8;
					m_pXA->SFXPlaySound(m_nLoseSfxID,false);
				}
			}
	}
	else
	{
		m_fGameOverTimer-=fElapsedTime;
	}
}	

void CGameplayState::Render( void )
{
	CGame* gamePtr = CGame::GetInstance();
	//Render Background
	m_pTM->Draw(m_nBackgroundID,-gamePtr->GetCameraX(),-gamePtr->GetCameraY(),WORLD_SCALE,WORLD_SCALE,nullptr,
		(gamePtr->GetCameraX()+gamePtr->GetWidth()*0.5f)/WORLD_SCALE,(gamePtr->GetCameraY()+gamePtr->GetHeight()*0.5f)/WORLD_SCALE,gamePtr->GetCameraRotation(),D3DCOLOR_ARGB(255,255,255,255));
	m_pD3D->GetSprite()->Flush();
	m_pEM->RenderAll();
	if(CGame::DEBUG_MODE)
		RenderDebugs();
	m_pD3D->GetSprite()->Flush();
	
	parallaxImage->Render();
	m_pD3D->GetSprite()->Flush();

	RenderProgressBar();
	RenderMinimap();
	RenderPlayerPlacement();


	if(m_nGameOver == 1)
		RenderWinningScreen();
	else if(m_nGameOver == 2)
		RenderLosingScreen();
}			

void CGameplayState::RenderPlayerPlacement()
{
	int placement = 1;
	for(unsigned int i = 0; i < shipVector.size();++i)
	{
		if(((CShip*)shipVector[i])->GetTargetWaypoint()>((CShip*)m_pShip)->GetTargetWaypoint())
			++placement;
	}

	int startY = 32;
	int startX = 32;
	int offset = 30;
	TOSTRINGSTREAM output;
	
	if(placement==1)
		output<<"1st";
	else if(placement==2)
		output<<"2nd";
	else if(placement==3)
		output<<"3rd";
	else if(placement==4)
		output<<"4th";
	else if(placement==5)
		output<<"5th";

	output << _T(" Place ");
	m_pFont->Draw( output.str().c_str(), startX, startY+=offset, 0.8f, D3DCOLOR_ARGB(255, 255, 255, 255));
	output.str(_T(""));
	
	//score
	output << _T("Score: ") << CGame::GetInstance()->GetScore();
	m_pFont->Draw( output.str().c_str(), startX, startY+=offset, 0.8f, D3DCOLOR_ARGB(255, 255, 255, 255));
	output.str(_T(""));

}

void CGameplayState::RenderWinningScreen()
{
	CGame* gamePtr = CGame::GetInstance();
	m_pTM->Draw(m_nWinScreenID,0,0,gamePtr->GetWidth()/1024.0f,gamePtr->GetHeight()/1024.0f,nullptr,0,0,0,D3DCOLOR_ARGB(200,255,255,255));
	m_pD3D->GetSprite()->Flush();
}

void CGameplayState::RenderLosingScreen()
{
	CGame* gamePtr = CGame::GetInstance();
	m_pTM->Draw(m_nLoseScreenID,0,0,gamePtr->GetWidth()/1024.0f,gamePtr->GetHeight()/1024.0f,nullptr,0,0,0,D3DCOLOR_ARGB(200,255,255,255));
	m_pD3D->GetSprite()->Flush();
}

void CGameplayState::RenderProgressBar()
{
	CGame* gamePtr = CGame::GetInstance();
	int startX = 32;
	int startY = gamePtr->GetHeight()- 64;
	int endX =  gamePtr->GetWidth()- 256 - 32;
	int endY =  gamePtr->GetHeight()-32;

	int scaleX = (endX-startX);

	m_pTM->Draw(m_nWhiteDotID,startX,startY,(endX-startX)/512.0f,(endY-startY)/32.0f,nullptr,0,0,0,D3DCOLOR_ARGB(255,255,255,255));
	m_pTM->Draw(m_nGoalID,endX-32,startY-32,32.0f/512.0f,32.0f/512.0f,nullptr,0,0,0,D3DCOLOR_ARGB(255,255,255,255));

	m_pD3D->GetSprite()->Flush();

	for(unsigned int i = 0; i < shipVector.size();++i)
	{
		float _nX = ((CShip*)shipVector[i])->GetTargetWaypoint()/(float)CEntityManager::GetInstance()->GetWayPointSize();
		int _nY = startY;

		_nX*=scaleX;
		_nX+=startX;

		//RECT r = {_nX-12,_nY-12,_nX+12,_nY+12};
		//if(shipVector[i]->GetType()==CEntity::ENT_INPUTSHIP)
		//	m_pD3D->DrawRect(r,255,0,0);
		//else
		//	m_pD3D->DrawRect(r,0,0,255);

		m_pTM->Draw(shipVector[i]->GetImageID(),(int)_nX-12,_nY-12,24/512.0f,24/512.0f,nullptr,0,0,0,D3DCOLOR_ARGB(255,255,255,255));

	}
}


void CGameplayState::RenderMinimap()
{
	CGame* gamePtr = CGame::GetInstance();
	int offX = gamePtr->GetWidth()-256;
	int offY = gamePtr->GetHeight()-256;

	m_pTM->Draw(m_nBackgroundID,offX,offY,256/2056.0f,256/2056.0f,nullptr,0,0,0,D3DCOLOR_ARGB(150,255,255,255));
	m_pD3D->GetSprite()->Flush();

	for(int i = 0; i < 128; ++i)
	{
		for(int j = 0; j < 128; ++j)
		{
			RECT r = {i+i+offX,j+j+offY,i+i+offX+1,j+j+offY+1};
			if(m_ucMiniMap[i][j])
				m_pD3D->DrawRect(r,255,255,255);
		}
	}
	int tilesize = (int)(16*WORLD_SCALE);

	for(unsigned int i = 0; i < shipVector.size();++i)
	{
		int _nX = (int)shipVector[i]->GetPosX();
		int _nY = (int)shipVector[i]->GetPosY();

		_nX/=tilesize;
		_nY/=tilesize;

		_nX*=2;
		_nY*=2;

		_nX+=offX;
		_nY+=offY;
		/*
		RECT r = {_nX-2,_nY-2,_nX+2,_nY+2};
		if(shipVector[i]->GetType()==CEntity::ENT_INPUTSHIP)
		m_pD3D->DrawRect(r,255,0,0);
		else
		m_pD3D->DrawRect(r,0,0,255);
		*/

		m_pTM->Draw(shipVector[i]->GetImageID(),_nX-12,_nY-12,24/512.0f,24/512.0f,nullptr,256,256,shipVector[i]->GetRotation(),D3DCOLOR_ARGB(255,255,255,255));

	}
}

void CGameplayState::RenderDebugs()
{
	int startY = 50;
	int offset = 30;
	TOSTRINGSTREAM output;
	
	output << _T("FPS: ") << m_nFPS;
	m_pFont->Draw( output.str().c_str(), 0, startY+=offset, 0.5f, D3DCOLOR_ARGB(255, 255, 255, 255) );
	output.str(_T(""));

	output << _T("X: ") << m_pShip->GetPosX();
	m_pFont->Draw( output.str().c_str(), 0, startY+=offset, 0.5f, D3DCOLOR_ARGB(255, 255, 255, 255) );
	output.str(_T(""));

	output << _T("Y: ") << m_pShip->GetPosY();
	m_pFont->Draw( output.str().c_str(), 0, startY+=offset, 0.5f, D3DCOLOR_ARGB(255, 255, 255, 255) );
	output.str(_T(""));

	output << _T("Speed: ") << m_pShip->GetSpeed();
	m_pFont->Draw( output.str().c_str(), 0, startY+=offset, 0.5f, D3DCOLOR_ARGB(255, 255, 255, 255) );
	output.str(_T(""));


}

void CGameplayState::UpdateFPS( float fElapsedTime )
{
	// Update the FPS timer
	m_fFPSTimer += fElapsedTime;
	if( m_fFPSTimer >= 1.0f )
	{
		m_nFPS		= m_nFrames;
		m_nFrames	= 0;
		m_fFPSTimer	= 0.0f;
	}
	++m_nFrames;
}

void CGameplayState::MessageProc( IMessage* pMsg )
{
	CGameplayState* self = CGameplayState::GetInstance();

	switch( pMsg->GetMessageID() )
	{
	case MSG_CREATE_LASER:
		{
			CEntity* pLaser = ((CCreateLaserMessage*)pMsg)->CreateLaser();
			if(abs(pLaser->GetPosX()-CGame::GetInstance()->GetCameraX()-CGame::GetInstance()->GetWidth()/2)<CGame::GetInstance()->GetWidth())
			{
				if(abs(pLaser->GetPosY()-CGame::GetInstance()->GetCameraY()-CGame::GetInstance()->GetHeight()/2)<CGame::GetInstance()->GetHeight())
				{
					self->m_pXA->SFXStopSound( self->m_nLaserShotSfxID );
					self->m_pXA->SFXPlaySound( self->m_nLaserShotSfxID );
				}
			}
			pLaser->SetImageID( self->m_nLaserImageID );
			self->m_pEM->AddEntity( pLaser );
			pLaser->Release();
			pLaser = nullptr;
		}
		break;
	case MSG_CREATE_SPREAD_LASER:
		{
			CCreateSpreadLaserMessage* mess_ptr = ((CCreateSpreadLaserMessage*)pMsg);
			mess_ptr->Process();
			if(abs(mess_ptr->GetPosX()-CGame::GetInstance()->GetCameraX()-CGame::GetInstance()->GetWidth()/2)<CGame::GetInstance()->GetWidth())
			{
				if(abs(mess_ptr->GetPosY()-CGame::GetInstance()->GetCameraY()-CGame::GetInstance()->GetHeight()/2)<CGame::GetInstance()->GetHeight())
				{
					self->m_pXA->SFXStopSound( self->m_nLaserShotSfxID );
					self->m_pXA->SFXPlaySound( self->m_nLaserShotSfxID );
				}
			}
		}
		break;
	case MSG_CREATE_ENEMY:
		{
			CEntity* pEntity = ((CCreateEnemyMessage*)pMsg)->CreateEnemy();
			pEntity->SetImageID(self->m_nShipImageID[rand()%5]);
			self->m_pEM->AddEntity( pEntity );
			pEntity->Release();
			pEntity = nullptr;
		}
		break;
	case MSG_DESTROY_ENTITY:
		{
			self->m_pEM->RemoveEntity(((CDestroyEntityMessage*)pMsg)->GetEntity());
		}
		break;
	default:
		assert( false && "CGame::MessageProc - unknown message type" );
	}
}
